package com.epfresh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.api.utils.T;
import com.epfresh.bean.CartGood;
import com.epfresh.bean.CartGoods;
import com.epfresh.bean.OfferMarkBean;
import com.epfresh.global.BaseApplication;
import com.epfresh.utils.FormatUtil;
import com.epfresh.views.tag.TagCloudView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wzh on 2016/5/6.
 */
public class OfferExpandListAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    public interface OnCheckChangeListener {
        public void onCheckChange(boolean isCheck, int groupPosition, int childPosition);
    }

    public interface OnTextChangeListener {
        public void onTextChange(int groupPosition, int childPosition, double lastCnt, OfferMarkBean.ProductListBean cartGood);
    }

    public Context context;
    public ExpandableListView lv;

    public List<OfferMarkBean> groupList;


    private AdapterView.OnItemClickListener onItemClickListener;
    private OnBottomClickListener onBottomClickListener;
    private OnNotifyDataSetChangedListener onNotifyDataSetChangedListener;
    private OnCheckChangeListener onCheckChangeListener;
    private OnTextChangeListener onTextChangeListener;
    private OnTextChangeListener onChangeListener;
    private OnCountListener onCountListener;

    public OnCountListener getOnCountListener() {
        return onCountListener;
    }

    public void setOnCountListener(OnCountListener onCountListener) {
        this.onCountListener = onCountListener;
    }

    public OfferExpandListAdapter(Context context, ExpandableListView lv, List<OfferMarkBean> groupList) {
        this.context = context;
        this.groupList = groupList;
        this.lv = lv;
    }

    public OnTextChangeListener getOnTextChangeListener() {
        return onTextChangeListener;
    }

    public void setOnTextChangeListener(OnTextChangeListener onTextChangeListener) {
        this.onTextChangeListener = onTextChangeListener;
    }

    public OnCheckChangeListener getOnCheckChangeListener() {
        return onCheckChangeListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    public OnTextChangeListener getOnChangeListener() {
        return onChangeListener;
    }

    public void setOnChangeListener(OnTextChangeListener onChangeListener) {
        this.onChangeListener = onChangeListener;
    }

    @Override
    public void notifyDataSetChanged() {
        if (onNotifyDataSetChangedListener != null) {
            onNotifyDataSetChangedListener.OnNotifyDataSetChangedListener(groupList.size());
        }
        expendList();
        super.notifyDataSetChanged();
    }


    public OnNotifyDataSetChangedListener getOnNotifyDataSetChangedListener() {
        return onNotifyDataSetChangedListener;
    }

    public void setOnNotifyDataSetChangedListener(OnNotifyDataSetChangedListener onNotifyDataSetChangedListener) {
        this.onNotifyDataSetChangedListener = onNotifyDataSetChangedListener;
    }

    public OnBottomClickListener getOnBottomClickListener() {
        return onBottomClickListener;
    }

    public void setOnBottomClickListener(OnBottomClickListener onBottomClickListener) {
        this.onBottomClickListener = onBottomClickListener;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int count = 0;
        OfferMarkBean cartGoods = groupList.get(groupPosition);
        List<OfferMarkBean.ProductListBean> configs = cartGoods.getProductList();

        if (configs != null) {
            count = configs.size();
        }
/*        if ( !cartGoods.isDisabled() && count > 3 && !cartGoods.isExpend()) {
            count = 3;
        }*/


        return count;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return groupList.get(groupPosition).getProductList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
     *
     * @return
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_list_cart, null, false);

            vh.iv = (ImageView) convertView.findViewById(R.id.iv_box);
            vh.tv = (TextView) convertView.findViewById(R.id.tv_name);
            vh.vLine = convertView.findViewById(R.id.v_line);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        convertView.setTag(R.id.item_key_position, groupPosition);
        convertView.setClickable(true);
        OfferMarkBean cartGoods = groupList.get(groupPosition);
        if (cartGoods.getProductList() == null || cartGoods.getProductList().size() == 0) {
            vh.vLine.setVisibility(View.GONE);
        } else {
            vh.vLine.setVisibility(View.VISIBLE);

        }
        checkBoxState(cartGoods.isSelect(), vh.iv);
        vh.tv.setText(cartGoods.getMarketName());
        vh.iv.setTag(R.id.item_key_position, groupPosition);
        vh.iv.setOnClickListener(this);
        return convertView;
    }


    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild   子元素是否处于组中的最后一个
     * @param convertView   重用已有的视图(View)对象
     * @param parent        返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View,
     * ViewGroup)
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        convertView = getChildNormalView(groupPosition, childPosition, isLastChild, convertView, parent);


        return convertView;
    }

    public View getChildNormalView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder vh = null;
        if (convertView == null) {
            int[]a = {1};
            int b = a.length;
            vh = new ChildViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_child_list_offer, null, false);
            vh.tv_car_offer_hint = convertView.findViewById(R.id.tv_car_offer_hint);
            vh.ivChildCheck = (ImageView) convertView.findViewById(R.id.iv_child_box);
            vh.ivLogo = (ImageView) convertView.findViewById(R.id.iv_logo);
            //现货,预售标志
            vh.tvType = (TextView) convertView.findViewById(R.id.tv_type);
            vh.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            vh.tvStore = (TextView) convertView.findViewById(R.id.tv_store);

            vh.tvUnit = (TextView) convertView.findViewById(R.id.tv_unit_package);
            vh.ivAdd = (ImageView) convertView.findViewById(R.id.iv_add);
            vh.ivRemove = (ImageView) convertView.findViewById(R.id.iv_remove);

            vh.edAmount = (TextView) convertView.findViewById(R.id.ed_price);
            vh.edAmountUnderlineLayout = (LinearLayout) convertView.findViewById(R.id.item_child_list_ed_price_underline_layout);
            vh.edAmountUnderlineTv=(TextView)convertView.findViewById(R.id.item_child_list_ed_price_underline_tv);
            vh.tvDisable = (TextView) convertView.findViewById(R.id.tv_disable);
            vh.vBacker =  convertView.findViewById(R.id.v_backer);
            vh.ivCorner = (ImageView) convertView.findViewById(R.id.iv_corner);

            vh.tvTotal = (TextView) convertView.findViewById(R.id.tv_total);
            vh.tvTotalName= (TextView) convertView.findViewById(R.id.tv_total_name);
            vh.tvCount = (TextView) convertView.findViewById(R.id.tv_btn);
            vh.vCount =  convertView.findViewById(R.id.rl_count);
            vh.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            vh.tv_price_ = (TextView) convertView.findViewById(R.id.tv_price_);
            vh.tv_price_old = (TextView) convertView.findViewById(R.id.tv_price_old);
            vh.tv_activity_promotion =  convertView.findViewById(R.id.tv_activity_promotion);
            vh.tv_activity_second = convertView.findViewById(R.id.tv_activity_second);
            vh.tvChargeNumber = (TextView) convertView.findViewById(R.id.tv_charge_number);

            convertView.setTag(vh);
        } else {
            vh = (ChildViewHolder) convertView.getTag();
        }


        OfferMarkBean cartGoods = groupList.get(groupPosition);
        List<OfferMarkBean.ProductListBean> goods = cartGoods.getProductList();
        OfferMarkBean.ProductListBean cartGood = goods.get(childPosition);


        checkBoxState(cartGood.isSelect(), vh.ivChildCheck);
        vh.tvTitle.setText(cartGood.getProductTitle());
        vh.tvStore.setText(cartGood.getStoreName());
        vh.tv_price.setText("¥"+cartGood.getProductUnitPrice()+"/"+cartGood.getProductChargeUnit());

        String productPackageSize = cartGood.getProductPackageSize();
        if (productPackageSize == null || "".equals(productPackageSize)){
            vh.tvUnit.setText("");
            vh.tvUnit.setVisibility(View.GONE);
        }else {
            vh.tvUnit.setText(productPackageSize);
            vh.tvUnit.setVisibility(View.VISIBLE);
        }

        //vh.tv_car_offer_hint.setVisibility(View.VISIBLE);
        vh.tv_price.setVisibility(View.VISIBLE);
        vh.tv_price_.setVisibility(View.VISIBLE);
        vh.tv_price_old.setVisibility(View.VISIBLE);



        if (childPosition == goods.size() - 1) {
//            vh.vLine.setVisibility(View.INVISIBLE);
        } else {
//            vh.vLine.setVisibility(View.VISIBLE);
        }

        vh.ivChildCheck.setTag(R.id.item_key_position, groupPosition);
        vh.ivChildCheck.setTag(R.id.item_key_status, childPosition);
        vh.ivChildCheck.setOnClickListener(this);

        vh.ivAdd.setTag(R.id.item_key_position, groupPosition);
        vh.ivAdd.setTag(R.id.item_key_status, childPosition);
        vh.ivAdd.setTag(R.id.item_key_view, vh.edAmount);
        vh.ivAdd.setOnClickListener(this);

        vh.ivRemove.setTag(R.id.item_key_position, groupPosition);
        vh.ivRemove.setTag(R.id.item_key_status, childPosition);
        vh.ivRemove.setTag(R.id.item_key_view, vh.edAmount);
        vh.ivRemove.setOnClickListener(this);

        vh.edAmount.setTag(R.id.item_key_position, groupPosition);
        vh.edAmount.setTag(R.id.item_key_status, childPosition);


        vh.edAmount.setText(FormatUtil.subZeroAndDot(cartGood.getProductCount() + ""));
        vh.edAmountUnderlineTv.setText(FormatUtil.subZeroAndDot(cartGood.getProductCount() + ""));
        vh.tvChargeNumber.setText("已选"+FormatUtil.subZeroAndDot(cartGood.getProductCount() + "")+cartGood.getProductChargeUnit() + "");
        vh.edAmount.setOnClickListener(this);
        vh.vMore = convertView.findViewById(R.id.rl_more);
        vh.ivMore = (ImageView) convertView.findViewById(R.id.iv_more);
        vh.tvMore = (TextView) convertView.findViewById(R.id.tv_more);


        ImageLoader.getInstance().displayImage(cartGood.getProductImgUrl(), vh.ivLogo);

        convertView.setTag(R.id.item_key_position, groupPosition);
        convertView.setTag(R.id.item_key_status, childPosition);
        convertView.setOnClickListener(onClickListener);
        convertView.setOnLongClickListener(onLongClickListener);
        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                mRawX = event.getRawX();
                mRawY = event.getRawY();

                return false;
            }
        });

        return convertView;
    }
    public void check(int fatherPosition, int childPosition){
        OfferMarkBean cartGoods1 = groupList.get(fatherPosition);
        List<OfferMarkBean.ProductListBean> goods = cartGoods1.getProductList();

        OfferMarkBean.ProductListBean cartGood = goods.get(childPosition);
        cartGood.setSelect(true);
        checkParentStatus(cartGoods1, goods);
        if (onCheckChangeListener != null) {
            onCheckChangeListener.onCheckChange(checkAllStatus(), fatherPosition, childPosition);
        }
    }

    public Pair<Integer, Double> getSelectTotalPrice(OfferMarkBean cartGoods){
        int cnt = 0;
        double total = 0d;
        if (cartGoods != null){
/*

            if (isEdit || cartGoods.isDisabled()) {
                return new Pair<>(cnt, total);
            }
*/

            List<OfferMarkBean.ProductListBean> list = cartGoods.getProductList();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    OfferMarkBean.ProductListBean cartGood = list.get(i);
                    if (cartGood.isSelect()) {
                        cnt++;
                    }
                }
            }
        }



        return new Pair<>(cnt, total);
    }

    public float mRawX;
    public float mRawY;
    ExpandableListView.OnChildClickListener onChildClickListener;
    ExpandableListView.OnChildClickListener onChildLongClickListener;

    public ExpandableListView.OnChildClickListener getOnChildLongClickListener() {
        return onChildLongClickListener;
    }

    public void setOnChildLongClickListener(ExpandableListView.OnChildClickListener onChildLongClickListener) {
        this.onChildLongClickListener = onChildLongClickListener;
    }

    public ExpandableListView.OnChildClickListener getOnChildClickListener() {
        return onChildClickListener;
    }

    public void setOnChildClickListener(ExpandableListView.OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }

    private View.OnClickListener onCountClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Object o1 = view.getTag(R.id.item_key_status);
            Object o2 = view.getTag(R.id.item_key_view);
//            vh.tvCount.setTag(R.id.item_key_status,isUnable);
//            vh.tvCount.setTag(R.id.item_key_view,cartGoods);

            if (onCountListener != null&& o1!=null&& o2!=null && o1 instanceof Boolean && o2 instanceof OfferMarkBean){
                onCountListener.onCountListener((OfferMarkBean) o2,(Boolean) o1);
            }


        }
    };/*
    private View.OnClickListener onMoreClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Object o2 = view.getTag(R.id.item_key_view);
//            vh.tvCount.setTag(R.id.item_key_status,isUnable);
//            vh.tvCount.setTag(R.id.item_key_view,cartGoods);

            if (o2!=null  && o2 instanceof OfferMarkBean){
                OfferMarkBean cartGoods7 = (OfferMarkBean) o2;
                boolean expend = cartGoods7.isExpend();
                cartGoods7.setExpend(!expend);
                notifyDataSetChanged();
            }


        }
    };*/
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Object o1 = view.getTag(R.id.item_key_position);
            Object o2 = view.getTag(R.id.item_key_status);

            int groupPosition = (int) o1;
            int childPosition = (int) o2;
            if (onChildClickListener != null) {
                onChildClickListener.onChildClick(lv, view, groupPosition, childPosition, groupPosition);
            }

        }
    };
    private View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {

            Object o1 = view.getTag(R.id.item_key_position);
            Object o2 = view.getTag(R.id.item_key_status);

            int groupPosition = (int) o1;
            int childPosition = (int) o2;
            if (onChildLongClickListener != null) {
                onChildLongClickListener.onChildClick(lv, view, groupPosition, childPosition, groupPosition);
            }

            return true;
        }
    };
    public interface OnMsgAddListener {
        void onMsgAdd(int position, CartGood goods);
    }
    OnMsgAddListener onMsgAddListener;

    public OnMsgAddListener getOnMsgAddListener() {
        return onMsgAddListener;
    }

    public void setOnMsgAddListener(OnMsgAddListener onMsgAddListener) {
        this.onMsgAddListener = onMsgAddListener;
    }

    @Override
    public void onClick(View v) {


        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;

            switch (v.getId()) {
                case R.id.tv_msg:
                    Object o = v.getTag(R.id.item_key_status);

                    if (onMsgAddListener != null&& o instanceof CartGood){
                        onMsgAddListener.onMsgAdd(position,(CartGood) o);
                    }
                    break;

                case R.id.iv_box:
                    OfferMarkBean cartGoods = groupList.get(position);
                    cartGoods.setSelect(!cartGoods.isSelect());
                    checkBoxState(cartGoods.isSelect(), (ImageView) v);

                    checkAllChildStatus(cartGoods.getProductList(), cartGoods.isSelect());

                    if (onCheckChangeListener != null) {
                        onCheckChangeListener.onCheckChange(checkAllStatus(), position, -1);
                    }
                    break;

                case R.id.iv_child_box:
                    Object tag1 = v.getTag(R.id.item_key_status);

                    if (tag1 instanceof Integer) {
                        int childPosition = (int) tag1;

                        OfferMarkBean cartGoods1 = groupList.get(position);
                        List<OfferMarkBean.ProductListBean> goods = cartGoods1.getProductList();

                        OfferMarkBean.ProductListBean cartGood = goods.get(childPosition);
                        cartGood.setSelect(!cartGood.isSelect());

                        checkBoxState(cartGood.isSelect(), (ImageView) v);
                        //如果子类变为非选中状态,则更新父类状态
                        if (!cartGood.isSelect()) {
                            cartGoods1.setSelect(false);
                            notifyDataSetChanged();
                        } else {
                            checkParentStatus(cartGoods1, goods);
                        }
                        if (onCheckChangeListener != null) {
                            onCheckChangeListener.onCheckChange(checkAllStatus(), position, childPosition);
                        }
                    }
                    break;
                case R.id.iv_add:
                    Object tag2 = v.getTag(R.id.item_key_status);

                    if (tag2 instanceof Integer) {
                        int childPosition = (int) tag2;
                        check(position,childPosition);
                        TextView tv = (TextView) v.getTag(R.id.item_key_view);


                        OfferMarkBean cartGoods1 = groupList.get(position);
                        List<OfferMarkBean.ProductListBean> goods = cartGoods1.getProductList();
                        if (childPosition >= goods.size()) {
                            return;
                        }
                        OfferMarkBean.ProductListBean cartGood = goods.get(childPosition);

                        double amount = cartGood.getProductCount();
                        double lastAmount = cartGood.getProductCount();
                        //double min = cartGood.getMoq();todo该字段没了？todo:2018年11月16日13:34:59
                        double min = cartGood.getIncr();
                        double incr =  cartGood.getIncr();
                        if (amount < min) {
                            amount = min;
                        }
                        amount = FormatUtil.countAdd(amount,incr);
                        cartGood.setProductCount(amount);
                        tv.setText(FormatUtil.subZeroAndDot(amount + ""));

                        if (onChangeListener != null) {
                            onChangeListener.onTextChange(position, childPosition, lastAmount, cartGood);
                        }

                    }
                    break;
                case R.id.iv_remove:
                    Object tag3 = v.getTag(R.id.item_key_status);

                    if (tag3 instanceof Integer) {
                        int childPosition = (int) tag3;
                        check(position,childPosition);

                        TextView tv = (TextView) v.getTag(R.id.item_key_view);


                        OfferMarkBean cartGoods1 = groupList.get(position);
                        List<OfferMarkBean.ProductListBean> goods = cartGoods1.getProductList();
                        OfferMarkBean.ProductListBean cartGood = goods.get(childPosition);

                        double amount = cartGood.getProductCount();
                        double lastAmount = cartGood.getProductCount();
                        double min = cartGood.getMoq();
                        double incr =  cartGood.getIncr();
                        amount = FormatUtil.countSubtract(amount,incr);

                        if (amount < min) {
                            amount = min;
                            T.toast("问价数量不能小于起订量"+cartGood.getMoq());
                        }
                        cartGood.setProductCount(amount);
                        tv.setText(FormatUtil.subZeroAndDot(amount + ""));
                        if (onChangeListener != null) {
                            onChangeListener.onTextChange(position, childPosition, lastAmount, cartGood);
                        }
                    }
                    break;
                case R.id.ed_price:
                    Object tag4 = v.getTag(R.id.item_key_status);

                    if (tag4 instanceof Integer) {
                        int childPosition = (int) tag4;

                        TextView tv = (TextView) v.getTag(R.id.item_key_view);


                        OfferMarkBean cartGoods1 = groupList.get(position);
                        List<OfferMarkBean.ProductListBean> goods = cartGoods1.getProductList();
                        OfferMarkBean.ProductListBean cartGood = goods.get(childPosition);

                        if (onTextChangeListener != null) {
                            onTextChangeListener.onTextChange(position, childPosition, cartGood.getProductCount(), cartGood);
                        }

                    }
                    break;

            }


        }
    }


    public void editState() {
        for (int i = 0; i < groupList.size(); i++) {
            OfferMarkBean cartGoods = groupList.get(i);
            if (cartGoods != null) {
                List<OfferMarkBean.ProductListBean> result = cartGoods.getProductList();
                if (result != null) {
                    for (int j = 0; j < result.size(); j++) {
                        OfferMarkBean.ProductListBean cartGood = result.get(j);
                        cartGood.storedSelect();
                    }
                    checkParentStatus(cartGoods, result);
                }
            }
        }


    }

    public void releaseStatus() {
        for (int i = 0; i < groupList.size(); i++) {
            OfferMarkBean cartGoods = groupList.get(i);
            if (cartGoods != null) {
                List<OfferMarkBean.ProductListBean> result = cartGoods.getProductList();
                if (result != null) {
                    for (int j = 0; j < result.size(); j++) {
                        OfferMarkBean.ProductListBean cartGood = result.get(j);
                        cartGood.releaseSelect();
                    }
                    checkParentStatus(cartGoods, result);
                }
            }
        }


    }


    public void checkBoxState(boolean isCheck, ImageView iv) {
        if (isCheck) {
            //全选
            iv.setImageResource(R.drawable.select_round);

        } else {
            //反选
            iv.setImageResource(R.drawable.register_unread);
        }
    }

    public void checkAllChildStatus(List<OfferMarkBean.ProductListBean> list, boolean isCheck) {

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                OfferMarkBean.ProductListBean cartGood = list.get(i);
                cartGood.setSelect(isCheck);
            }
            notifyDataSetChanged();
        }
    }

    public void checkParentStatus(OfferMarkBean cartGoods, List<OfferMarkBean.ProductListBean> list) {

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                OfferMarkBean.ProductListBean cartGood = list.get(i);
                    if (!cartGood.isSelect()) {
                        cartGoods.setSelect(false);
                        notifyDataSetChanged();
                        return;
                    }

            }
            cartGoods.setSelect(true);
        }else {
            cartGoods.setSelect(false);
        }
        notifyDataSetChanged();
    }

    public void checkStatus() {

        for (int i = 0; i < groupList.size(); i++) {
            OfferMarkBean cartGoods = groupList.get(i);
            List<OfferMarkBean.ProductListBean> result = cartGoods.getProductList();
            checkParentStatus(cartGoods, result);
        }
    }

    public void checkAllStatus(boolean isCheck) {
        for (int j = 0; j < groupList.size(); j++) {

            OfferMarkBean cartGoods = groupList.get(j);

            cartGoods.setSelect(isCheck);
            List<OfferMarkBean.ProductListBean> list = cartGoods.getProductList();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    OfferMarkBean.ProductListBean cartGood = list.get(i);
                    cartGood.setSelect(isCheck);

                }
            }
        }
        notifyDataSetChanged();

    }

    public boolean checkAllStatus() {
        boolean isCheck = true;
        boolean isOnlyDisabledCartGoods = true;
        for (int j = 0; j < groupList.size(); j++) {

            OfferMarkBean cartGoods = groupList.get(j);

            isOnlyDisabledCartGoods = false;
            if (!cartGoods.isSelect()) {
                return false;
            }
            List<OfferMarkBean.ProductListBean> list = cartGoods.getProductList();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    OfferMarkBean.ProductListBean cartGood = list.get(i);
                    if (!cartGood.isSelect()) {
                        return false;
                    }
                }
            }
        }

        return isCheck && (!isOnlyDisabledCartGoods);
    }

    public Pair<Integer, Double> calAllCheckedStatus() {
        int cnt = 0;
        double total = 0d;
        for (int j = 0; j < groupList.size(); j++) {

            OfferMarkBean cartGoods = groupList.get(j);

            List<OfferMarkBean.ProductListBean> list = cartGoods.getProductList();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    OfferMarkBean.ProductListBean cartGood = list.get(i);
                    if (cartGood.isSelect()) {
                        cnt++;


                    }
                }
            }
        }

        return new Pair<>(cnt, total);
    }

    public List<String> getAllCheckedStatus() {
        List<String> listChecked = new ArrayList<>();
        for (int j = 0; j < groupList.size(); j++) {

            OfferMarkBean cartGoods = groupList.get(j);

            List<OfferMarkBean.ProductListBean> list = cartGoods.getProductList();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    OfferMarkBean.ProductListBean cartGood = list.get(i);
                    if (cartGood.isSelect()) {
                        listChecked.add(cartGood.getProductId() + "");
                    }
                }
            }
        }

        return listChecked;
    }

    public List<String> getAllDeleteCheckedStatus() {
        List<String> listChecked = new ArrayList<>();
        for (int j = 0; j < groupList.size(); j++) {

            OfferMarkBean cartGoods = groupList.get(j);

            List<OfferMarkBean.ProductListBean> list = cartGoods.getProductList();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    OfferMarkBean.ProductListBean cartGood = list.get(i);
                    if (cartGood.isSelect()) {
                        listChecked.add(cartGood.getProductId() + "");

                    }
                }
            }
        }

        return listChecked;
    }

    public List<Map<String, Object>> getSelectedStatus(int groupPosition) {
        List<Map<String, Object>> listChecked = new ArrayList<>();
        if (groupPosition < groupList.size()) {
            OfferMarkBean cartGoods = groupList.get(groupPosition);
            List<OfferMarkBean.ProductListBean> list = cartGoods.getProductList();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    OfferMarkBean.ProductListBean cartGood = list.get(i);

//                        "id": "1", "isSelected":"1" //1:选中; 0:选中
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", cartGood.getProductId());
                    map.put("isSelected", cartGood.getIsSelected());

                    listChecked.add(map);

                }
            }

        }
        return listChecked;
    }

    public List<Map<String, Object>> getAllSelectedStatus() {
        List<Map<String, Object>> listChecked = new ArrayList<>();
        for (int j = 0; j < groupList.size(); j++) {

            OfferMarkBean cartGoods = groupList.get(j);


            List<OfferMarkBean.ProductListBean> list = cartGoods.getProductList();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    OfferMarkBean.ProductListBean cartGood = list.get(i);

//                        "id": "1", "isSelected":"1" //1:选中; 0:选中
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", cartGood.getProductId());
                    map.put("isSelected", cartGood.getIsSelected());

                    listChecked.add(map);

                }
            }
        }

        return listChecked;
    }

    public List<Map<String, Object>> getSelectedStatus(int groupPosition, int childPosition) {
        List<Map<String, Object>> list = new ArrayList<>();

        //全部选中或者全部不选中
        if (groupPosition < 0) {
            return getAllSelectedStatus();

        } else if (childPosition < 0 && groupPosition < groupList.size()) {//选中/不选中某个市场的
            return getSelectedStatus(groupPosition);
        } else if (groupPosition < groupList.size() && groupList.get(groupPosition).getProductList() != null && childPosition < groupList.get(groupPosition).getProductList().size()) {
            //选中/不选中具体某个
            OfferMarkBean cartGoods = groupList.get(groupPosition);
            List<OfferMarkBean.ProductListBean> result = cartGoods.getProductList();
            if (result != null) {
                OfferMarkBean.ProductListBean cartGood = result.get(childPosition);
                if (cartGood != null) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", cartGood.getProductId());
                    map.put("isSelected", cartGood.getIsSelected());
                    list.add(map);
                }
            }
        }


        return list;
    }


    /**
     * 是否选中指定位置上的子元素。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    protected void expendList() {

        if (lv != null)
            for (int i = 0; i < groupList.size(); i++) {
                lv.expandGroup(i);
            }
    }

    public class EdUnitWatch implements TextWatcher {

        EditText ed;

        public EdUnitWatch(EditText ed) {
            this.ed = ed;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            int groupPosition = (int) ed.getTag(R.id.item_key_position);
            int childPosition = (int) ed.getTag(R.id.item_key_status);

            if (groupList.size() > groupPosition) {
                OfferMarkBean cartGoods = groupList.get(groupPosition);
                List<OfferMarkBean.ProductListBean> goods = cartGoods.getProductList();
                if (goods != null && goods.size() > childPosition) {
                    OfferMarkBean.ProductListBean cartGood = goods.get(childPosition);


                    double amount = 0;
                    if (s.length() > 0) {
                        amount = Integer.valueOf(s.toString());
                    } else {
                        amount = cartGood.getProductCount();
                    }/*

                    if (amount < cartGood.getMinPlaceOrderValue()) {
                        amount = cartGood.getMinPlaceOrderValue();
                    }
*/

                    cartGood.setProductCount(amount);
                }


            }

        }
    }

    public class ViewHolder {

        ImageView iv;
        TextView tv;
        View vLine;

    }

    public class ChildViewHolder {
        TextView tv_car_offer_hint;

        ImageView ivChildCheck;
        ImageView ivLogo;
        //现货,预售标志
        TextView tvType;
        TextView tvTitle;
        TextView tvStore;

        TextView tvChargeNumber;

        TextView tvUnit;
        //TextView tvUnitPrice;

        ImageView ivAdd;
        ImageView ivRemove;
        LinearLayout edAmountUnderlineLayout ;
        TextView edAmountUnderlineTv;
        TextView edAmount;

        //View vLine;
        View vBacker;

/*        TextView tvDiscount;
        TextView tvDiscountTitle;
        TextView tvDiscountRate;*/
        TextView tvDisable;

        //TextView tvPromotion;
//        TextView tvTag1;
//        TextView tvTag2;
        ImageView ivCorner;
        View vCount;

        TextView tvTotalName;
        TextView tvTotal;
        TextView tvCount;
        View vMore;
        TextView tvMore;
        ImageView ivMore;

        TextView tv_price,tv_price_,tv_price_old,tv_activity_promotion,tv_activity_second;

        TagCloudView tag;
    }


    public interface OnBottomClickListener {

        public void onBottomClickListener(int position, int type);
    }

    public interface OnNotifyDataSetChangedListener {

        public void OnNotifyDataSetChangedListener(int size);
    }
    public interface OnCountListener {

        public void onCountListener(OfferMarkBean cartGoods, boolean isUnable);
    }

    DeleteOne deleteOne;
    public interface DeleteOne{
        public void deleteOne(int deleteOne, int childPosition);
    }

    public DeleteOne getDeleteOne() {
        return deleteOne;
    }

    public void setDeleteOne(DeleteOne deleteOne) {
        this.deleteOne = deleteOne;
    }
}
